﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.VisualBasic.PowerPacks;
using System.Diagnostics;

namespace Galaxy_Wars
{
    partial class GameGui : Form
    {
        public Bitmap bitmapBackground { get; set; }
        private Bitmap bitmapMenuBar;

        public string playername { get; set; }
        public string playerID { get; set; }
        public int amountofplanets { get; set; }
        public int dayProduction { get; set; }
        public int turnduration { get; set; }
        public string ip { get; set; }
        public string gameType { get; set; }

        //private Graphics g;
        public Bitmap DrawArea { get; set; }

        private Game game;
        private SolidBrush brush;

        public Planet chosenPlanet { get; set; }
        public Planet actionPlanet { get; set; }
        
        //public GameGui(string playername, int amountofplanets, int amountofhips, int turnduration, string ip);


        public GameGui(string playername, string ip, string gameType)
        {
            InitializeComponent();

            this.startButton.Visible = false;

            this.playername = playername;
            this.ip = ip;
            this.gameType = gameType;

            //gives the form the size of the screen
            this.Size = Screen.PrimaryScreen.Bounds.Size;
            canvas.SetBounds(0, 0, this.Size.Width, (int)((this.Size.Height / 10) * 7.25));
            pictureBox1.SetBounds(0, canvas.Height, this.Width, (int)((this.Size.Height / 10) * 2.75));


            DrawArea = new Bitmap(canvas.Size.Width, canvas.Size.Height);
            canvas.Image = DrawArea;
        }

        public GameGui(string playername, int amountofplanets, int amountofhips, int turnduration, string ip, string gameType)
        {
            InitializeComponent();

            this.startButton.SetBounds(0, 0, this.Width, this.Height);
            this.startButton.Font = new Font("OCR A STD", 72);

            this.playername = playername;
            this.amountofplanets = amountofplanets;
            this.dayProduction = dayProduction;
            this.turnduration = turnduration;
            this.ip = ip;
            this.gameType = gameType;

            Console.WriteLine("Gametype = " + gameType);

            //gives the form the size of the screen
            this.Size = Screen.PrimaryScreen.Bounds.Size;
            canvas.SetBounds(0, 0, this.Size.Width, (int) ((this.Size.Height / 10) * 7.25));
            pictureBox1.SetBounds(0, canvas.Height, this.Width, (int) ((this.Size.Height / 10) * 2.75));


            DrawArea = new Bitmap(canvas.Size.Width, canvas.Size.Height);
            canvas.Image = DrawArea;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Make the Background image fit perfectly and set as the background image
            Image backgroundImage = Image.FromFile("Galaxy Img/Backgrounds/GalaxyBackground2.PNG");
            bitmapBackground = new Bitmap(backgroundImage, ClientSize.Width, ClientSize.Height);
            this.BackgroundImage = bitmapBackground;

            //Make the menubar background fit perfectly and set as the menubar background
            Image backgroundMenuBar = Image.FromFile("Galaxy Img/MenuBars/GalaxyBar8.PNG");
            bitmapMenuBar = new Bitmap(backgroundMenuBar, ClientSize.Width, ClientSize.Height);

            pictureBox1.BackgroundImage = bitmapMenuBar;

            chosenPlanetLabel.SetBounds(pictureBox1.Bounds.X + 50, pictureBox1.Bounds.Y + 50, chosenPlanetLabel.Width, chosenPlanetLabel.Height);
            sendShipsLabel.SetBounds((pictureBox1.Bounds.Width / 10) * 1, pictureBox1.Bounds.Y + 100, sendShipsLabel.Width, sendShipsLabel.Height);
                sendShipsUnderLabel1.SetBounds(sendShipsLabel.Bounds.X, sendShipsLabel.Bounds.Y + 30, sendShipsLabel.Bounds.Width, sendShipsLabel.Bounds.Height);
                sendShipsUnderLabel2.SetBounds(sendShipsLabel.Bounds.X, sendShipsLabel.Bounds.Y + 60, sendShipsLabel.Bounds.Width, sendShipsLabel.Bounds.Height);
                sendShipsUnderLabel3.SetBounds(sendShipsLabel.Bounds.X, sendShipsLabel.Bounds.Y + 90, sendShipsLabel.Bounds.Width, sendShipsLabel.Bounds.Height);
                sendShipsTextbox.SetBounds(sendShipsUnderLabel1.Bounds.X + sendShipsUnderLabel1.Width, sendShipsUnderLabel1.Bounds.Y, sendShipsTextbox.Width, sendShipsUnderLabel1.Height);

            sendMessageLabel.SetBounds((pictureBox1.Bounds.Width / 10) * 3, pictureBox1.Bounds.Y + 100, sendMessageLabel.Width, sendMessageLabel.Height);
                sendMessageUnderLabel1.SetBounds(sendMessageLabel.Bounds.X, sendMessageLabel.Bounds.Y + 30, sendMessageLabel.Bounds.Width, sendMessageLabel.Bounds.Height);
                sendMessageUnderLabel2.SetBounds(sendMessageLabel.Bounds.X, sendMessageLabel.Bounds.Y + 60, sendMessageLabel.Bounds.Width, sendMessageLabel.Bounds.Height);
                sendMessageUnderLabel3.SetBounds(sendMessageLabel.Bounds.X, sendMessageLabel.Bounds.Y + 90, sendMessageLabel.Bounds.Width, sendMessageLabel.Bounds.Height);

            retrieveMessageLabel.SetBounds((pictureBox1.Bounds.Width / 10) * 5, pictureBox1.Bounds.Y + 100, retrieveMessageLabel.Width, retrieveMessageLabel.Height);
                retrieveMessageUnderLabel1.SetBounds(retrieveMessageLabel.Bounds.X, retrieveMessageLabel.Bounds.Y + 30, retrieveMessageLabel.Bounds.Width, retrieveMessageLabel.Bounds.Height);
                retrieveMessageUnderLabel2.SetBounds(retrieveMessageLabel.Bounds.X, retrieveMessageLabel.Bounds.Y + 60, retrieveMessageLabel.Bounds.Width, retrieveMessageLabel.Bounds.Height);
                retrieveMessageUnderLabel3.SetBounds(retrieveMessageLabel.Bounds.X, retrieveMessageLabel.Bounds.Y + 90, retrieveMessageLabel.Bounds.Width, retrieveMessageLabel.Bounds.Height);



            showMenuItems(false);

            //g = Graphics.FromImage(DrawArea);

            game = new Game(this);
        }

        /// <summary>
        /// controls the display of the menu labels
        /// </summary>
        /// <param name="show">Boolean that indicates if the labels should be shown (true) or not (false)</param>
        public void showMenuItems(Boolean show)
        {
            chosenPlanetLabel.Visible = show;
            sendShipsLabel.Visible = show;
                sendShipsUnderLabel1.Visible = show;
                sendShipsUnderLabel2.Visible = show;
                sendShipsUnderLabel3.Visible = show;
                sendShipsTextbox.Visible = false;
            sendMessageLabel.Visible = show;
                sendMessageUnderLabel1.Visible = show;
                sendMessageUnderLabel2.Visible = show;
                sendMessageUnderLabel3.Visible = show;
            retrieveMessageLabel.Visible = show;
                retrieveMessageUnderLabel1.Visible = show;
                retrieveMessageUnderLabel2.Visible = show;
                retrieveMessageUnderLabel3.Visible = show;
        }

        public void setMenuItems(Planet p)
        {
            chosenPlanetLabel.Text = p.getName();

            Console.WriteLine("PlayerID = " + playerID + " PlayerName = " + playername);
            Console.WriteLine("owning playerID = " + p.getPlayernumber());

            if (p.getPlayernumber() + "" == playerID)
            {
                sendShipsLabel.Text = "Send Ships:";
                sendMessageLabel.Text = "Build Ships:";
                retrieveMessageLabel.Text = "Retrieve Messages:";
            }
            else
            {
                sendShipsLabel.Text = "Attack:";
                sendMessageLabel.Text = "Send Message:";
                retrieveMessageLabel.Text = "Can't Retrieve Someone Else's Messages:";
            }

            setMenuItemsContent(p);
             
        }

        public void setMenuItemsContent(Planet p)
        {
            if (sendShipsLabel.Text == "Send Ships:")
            {
                sendShipsUnderLabel1.Text = "how many ships:";

                sendShipsTextbox.Text = "";
                sendShipsTextbox.Visible = true;

                sendShipsUnderLabel2.Text = "Click Send Ships first";

                sendShipsUnderLabel3.Text = "After This Select The Planet";
            }
            if (sendShipsLabel.Text == "Attack:")
            {
                sendShipsUnderLabel1.Text = "---";

                sendShipsUnderLabel2.Text = "choose one of you planets from";

                sendShipsUnderLabel3.Text = "there you can attack this one";
            }
            if (sendMessageLabel.Text == "Build Ships:")
            {
                sendMessageUnderLabel1.Text = "Destroyer " + p.getAmountOfShips;

                //sendMessageUnderLabel2.Text = "Destroyer";
                sendMessageUnderLabel2.Visible = false;
                //sendMessageUnderLabel3.Text = "ITEM 3A";
                sendMessageUnderLabel3.Visible = false;
            }
            if (sendMessageLabel.Text == "Send Message:")
            {
                sendMessageUnderLabel1.Text = "Click Here To Send To This User";

                //sendMessageUnderLabel2.Text = "ITEM 2B";
                sendMessageUnderLabel2.Visible = false;
                //sendMessageUnderLabel3.Text = "ITEM 3B";
                sendMessageUnderLabel3.Visible = false;
            }
            if (retrieveMessageLabel.Text == "Retrieve Messages:")
            {
                retrieveMessageUnderLabel1.Text = "Check for new Messages";

                //retrieveMessageUnderLabel2.Text = "ITEM 2A";
                retrieveMessageUnderLabel2.Visible = false;
                //retrieveMessageUnderLabel3.Text = "ITEM 3A";
                retrieveMessageUnderLabel3.Visible = false;
            }
            if (retrieveMessageLabel.Text == "Can't Retrieve Someone Else's Messages:")
            {
                //retrieveMessageUnderLabel1.Text = "ITEM 1B";
                retrieveMessageUnderLabel1.Visible = false;
                //retrieveMessageUnderLabel2.Text = "ITEM 2B";
                retrieveMessageUnderLabel2.Visible = false;
                //retrieveMessageUnderLabel3.Text = "ITEM 3B";
                retrieveMessageUnderLabel3.Visible = false;
            }
        }

        /// <summary>
        /// resets the canvas.
        /// </summary>
        internal void resetCanvas()
        {
            if (chosenPlanet == null)
            {
                actionPlanet = null;
            }

            DrawArea = new Bitmap(canvas.Size.Width, canvas.Size.Height);
            canvas.Image = DrawArea;
        }

        /// <summary>
        /// Method for drawing individual planets
        /// </summary>
        /// <param name="planet">Planet to draw</param>
        public void drawPlanet(Planet planet)
        {
            Graphics g = Graphics.FromImage(DrawArea);
            g.DrawImage(planet.getPlanetLooks(), planet.getCoords());
            canvas.Invalidate(true);
        }

        /// <summary>
        /// Method which draws a enlarged image of the planet that is clicked.
        /// This method also makes sure the neccesary data is displayed below this
        /// enlarged image
        /// </summary>
        /// <param name="planet">Planet to draw bigger and with data</param>
        /// 

        public void drawError(string message) {
            Graphics g = Graphics.FromImage(DrawArea);
            brush = new SolidBrush(Color.Red);
            g.DrawString("Player: ", new Font("OCR A STD", 12), brush, new PointF(500, 500));
        }
        public void drawPlanetData(Planet planet)
        {
            Graphics g = Graphics.FromImage(DrawArea);
            brush = new SolidBrush(Color.Purple);

            ///////////test
            chosenPlanet = planet;

            int xModifier = 0;
            int yModifier = 0;

            showMenuItems(true);
            setMenuItems(planet);

            //This part checks if the bigger planet and data fall in screen. If they don't a coordinate modifier
            //is set to ensure a good position in screen.
            if (planet.getCoords().X > this.Width - 275)    xModifier = 275 - (this.Width - planet.getCoords().X);
            if (planet.getCoords().Y > (this.Height - pictureBox1.Height) - 100) yModifier = ((this.Height - pictureBox1.Height) - 100) - planet.getCoords().Y;
            if (planet.getCoords().Y < 120) yModifier = 120 - planet.getCoords().Y;
            Debug.WriteLine("XModifier " + xModifier + "YModifier " + yModifier);

            Rectangle rectTmp = new Rectangle(planet.getCoords().X + 75 - xModifier, planet.getCoords().Y - 35 + yModifier, 100, 100);
                        
            //this part draws the lines from the small representation of the planet
            //to the big planet with the data from that planet below it.
            g.DrawLine(new Pen(Color.Purple, 3), planet.getCoords().X + 25, planet.getCoords().Y , rectTmp.X, rectTmp.Y);
            g.DrawLine(new Pen(Color.Purple, 3), planet.getCoords().X + 25, planet.getCoords().Y + 25, rectTmp.X, rectTmp.Y + 100);

            //this part is to display all the planetData associated with the chosen planet.
            g.DrawString("Player: " + planet.getAll().Item1, new Font("OCR A STD", 12), brush, rectTmp.X, rectTmp.Y - 75);
            g.DrawString("Ships: " + planet.getAmountOfShips, new Font("OCR A STD", 12), brush, rectTmp.X, rectTmp.Y - 50);
            g.DrawString("Coords: " + planet.getAll().Item3.X + ", " + planet.getAll().Item3.Y, new Font("OCR A STD", 12), brush, rectTmp.X, rectTmp.Y - 25);
                

            g.DrawImage(planet.getPlanetLooks(), rectTmp);
            canvas.Invalidate(true);
        }

        /// <summary>
        /// Event, is triggered by clicking the playing field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            game.playingFieldClicked(e);
        }

        /// <summary>
        /// Event, is triggered by clicking in the menu on the downside of the screen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseClickMenu(object sender, MouseEventArgs e)
        {
            game.MenubarClicked(e);
        }

        private void onFormCloseEvent(object sender, FormClosingEventArgs e)
        {
            game.formClosingGoodbye();
        }

        private void endTurnClick(object sender, EventArgs e)
        {
            game.endTurn();
        }

        private void sendShipsLabel_Click(object sender, EventArgs e)
        {
            if (sendShipsLabel.Text == "Send Ships:")
            {
                actionPlanet = chosenPlanet;
            }
        }

        
        /// <summary>
        /// ensures there are no characters inserted other then numbers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sendShipsTextbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            } 
        }

        /// <summary>
        /// ensures that the user cannot try to send more ships then there are available
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sendShipsTextbox_TextChanged(object sender, EventArgs e)
        {
            string rawAmountOfShips = sendMessageUnderLabel1.Text;
            string amountOfShipsString = rawAmountOfShips.Substring(rawAmountOfShips.IndexOf(' ') + 1, rawAmountOfShips.Length - (rawAmountOfShips.IndexOf(' ') + 1));
            Console.WriteLine("Available ships: " + amountOfShipsString);

            ulong? tmpHuidigeWaardeTextfield = null;
            ulong? tmpAmountOfShips = null;
            try
            {
                tmpHuidigeWaardeTextfield = Convert.ToUInt64(sendShipsTextbox.Text);
                tmpAmountOfShips = Convert.ToUInt64(amountOfShipsString);
            }
            catch (Exception) { Console.WriteLine("failed to convert to integer"); }


            if ((tmpHuidigeWaardeTextfield > /**tmpAmountOfShips*/255) && sendShipsTextbox.Text.Length < 2)
            {
                Console.WriteLine("To many ships");
                sendShipsTextbox.Text = "";
            }
            else if (tmpHuidigeWaardeTextfield > /**tmpAmountOfShips*/255)
            {
                Console.WriteLine("To many ships selected");
                sendShipsTextbox.Text = sendShipsTextbox.Text.Substring(0, sendShipsTextbox.Text.Length - 1);
            }

            sendShipsTextbox.SelectionStart = sendShipsTextbox.Text.Length;
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            startButton.Visible = false;
            //stuur !START naar server
        }
    }
}
